<?php
namespace App\Domain\Phalapialiyunsms;

use App\Model\Phalapialiyunsms\Main as AliyunApi;
use PhalApi\Exception\BadRequestException;

class Main {
    
    // 接口调用
    public function aliyunSendSms($PhoneNumbers, $SignName, $TemplateCode, $TemplateParam) {
        $config = $this->takeAliyunConfig();
        
        try {
            return AliyunApi::init($config)->aliyunSendSms($PhoneNumbers, $SignName, $TemplateCode, $TemplateParam);
        } catch (\Exception $ex) {
            \PhalApi\DI()->logger->error('阿里云短信发送失败：' . $ex->getMessage());
            throw new BadRequestException($ex->getMessage());
        }
    }
    
    protected function takeAliyunConfig() {
        $cfgAll = \PhalApi\DI()->config->get('phalapi_aliyunsms.aliyun_sms');
        $accessKeyId = !empty($cfgAll['accessKeyId']) ? $cfgAll['accessKeyId'] : '';
        $accessSecret = !empty($cfgAll['accessSecret']) ? $cfgAll['accessSecret'] : '';
        
        if (empty($accessKeyId) || empty($accessSecret)) {
            throw new BadRequestException('阿里云短信服务未配置，请先修改./config/phalapi_aliyunsms.php配置文件');
        }
        
        $config = array(
            'accessKeyId' => $accessKeyId,
            'accessSecret' => $accessSecret,
        );
        
        return $config;
    }
    
    public function aliyunQuerySendDetails($CurrentPage, $PageSize, $PhoneNumber, $SendDate) {
        $config = $this->takeAliyunConfig();
        $apiRs = AliyunApi::init($config)->aliyunQuerySendDetails($CurrentPage, $PageSize, $PhoneNumber, $SendDate);
        
        return $apiRs;
    }
}
